package test;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * describe:
 *
 * @author helloworldyu
 * @date 2018/11/12
 */
public class FallbackCommand extends HystrixCommand<String> {
	private String commandName;

	protected FallbackCommand( String commandName ) {
		super(HystrixCommandGroupKey.Factory.asKey(FallbackCommand.class.getSimpleName()));
		this.commandName = commandName;
	}

	@Override
	protected String run() throws Exception {
		throw new RuntimeException(commandName + " 测试服务降级");
	}

	@Override
	protected String getFallback() {
		return commandName + " 降级之后的服务";
	}


	public static void main(String[] args) {
		testFailed();
	}

	private static void testFailed() {
		final int taskNum = 100;

		try {
			List<Future<String>> futures = new ArrayList<>(taskNum);
			for ( int i=0; i<taskNum; ++i ){
				Future<String> future = new FallbackCommand("FallbackCommand").queue();
				futures.add(future);
			}

			for( Future<String> future : futures ){
				String s = future.get();
				System.out.println(s);
			}

		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}

	}
}